package com.wanlian.netty.File.msg;

import com.wanlian.json.JsonStringUtil;
import com.wanlian.util.StringUtil;

import java.io.UnsupportedEncodingException;

/**
 * 文件信息
 */
public class FilePeerMsg {
    private FilePeerMsgHead head;
    private FileBaseMsg fileBaseMsg;

    public FilePeerMsg() {
        try {
            this.head = new FilePeerMsgHead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public FilePeerMsg(FileBaseMsg msg) {
        this.head = new FilePeerMsgHead();
        this.setFileBaseMsg(msg);
        try {
            String msgContent = msg.getContent();
            if (StringUtil.hasValue(msgContent)) {
                int msgLength = msgContent.getBytes("utf-8").length;
                this.head.setMsgLength(msgLength);
                if (this.fileBaseMsg.getData() != null && this.fileBaseMsg.getData().length > 0) {
                    this.head.setDataLength(this.fileBaseMsg.getData().length);
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public FilePeerMsg(FilePeerMsgHead head, FileBaseMsg msg) {
        this.setHead(head);
        this.setFileBaseMsg(msg);
    }


    public FilePeerMsgHead getHead() {
        return head;
    }

    public void setHead(FilePeerMsgHead head) {
        this.head = head;
    }

    public FileBaseMsg getFileBaseMsg() {
        return fileBaseMsg;
    }

    public void setFileBaseMsg(FileBaseMsg fileBaseMsg) {
        this.fileBaseMsg = fileBaseMsg;
    }

    public String toString() {
        return JsonStringUtil.writeValueAsString(this);
    }
}
